package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.pojo.seller.TbSeller;
import com.dk.service.seller.SellerService;
import com.dk.util.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("seller")
public class SellerController {

    private final Logger logger = LoggerFactory.getLogger(SellerController.class);

    @Reference
    private SellerService sellerService;
    @RequestMapping("toLogin")
    public String toLogin() {
        return "shoplogin";
    }

    @RequestMapping("toRegister")
    public String toRegister() {
        return "register";
    }

    @RequestMapping("sellerRegister")
    @ResponseBody
    public Result sellerRegister(TbSeller seller) {
        if (seller.getSellerId() == null || seller.getSellerId().equals("")) {
            return Result.response(ResultEnum.FAIL);
        }
        if (seller.getName() == null || seller.getName().equals("")) {
            return Result.response(ResultEnum.FAIL);
        }
        if (seller.getNickName() == null || seller.getNickName().equals("")) {
            return Result.response(ResultEnum.FAIL);
        }
        if (seller.getPassword() == null || seller.getPassword().equals("")) {
            return Result.response(ResultEnum.FAIL);
        }
        TbSeller tbSeller = sellerService.SellerBySellerId(seller.getSellerId());
        if (tbSeller != null) {
            return Result.response(ResultEnum.FAIL);
        }
        String salt = ShiroUtils.generateSalt(6);
        String md5 = ShiroUtils.encryptPassword("MD5", seller.getPassword(), salt, 3);
        seller.setSalte(salt);
        seller.setPassword(md5);
        try {
            int i = sellerService.insertSeller(seller);
            logger.info("商户注册成功,id为{}",seller.getSellerId());
            return Result.response(ResultEnum.OK);
        }catch (Exception e) {
            logger.error("商户注册失败",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("sellerLogin")
    public String sellerLogin(String sellerId, String password, Model model) {
        System.out.println(sellerId);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(sellerId,password);
        try {
            subject.login(token);
        }catch (UnknownAccountException unknownAccountException) {
            model.addAttribute("massage","用户名错误");
            return "shoplogin";
        } catch (IncorrectCredentialsException incorrectCredentialsException) {
            model.addAttribute("massage","密码错误");
            return "shoplogin";
        }
        return "admin/index";
    }
}
